package com.matt.controller.pad;

import com.matt.commons.dto.sso.AreaDTO;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.record.LiveRecord;
import com.matt.foundation.constant.RequestConstant;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.ModelUtil;
import com.matt.foundation.vo.liveRecord.BaseInfoVo;
import com.matt.foundation.vo.liveRecord.RecordListVo;
import com.matt.foundation.vo.observation.LiveRecordClassesVo;
import com.matt.service.LiveRecordService;
import com.matt.service.remoteCall.SsoCallService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.matt.foundation.constant.RequestConstant.TICKET;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/8/3
 * *******************************
 */
@RestController
@RequestMapping("/pad/live_record")
@Slf4j
public class PadLiveRecordController {

    @Autowired
    private LiveRecordService liveRecordService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private SsoCallService ssoCallService;


    @PostMapping("/add")
    public StatusResponse addLiveRecord(@RequestHeader(TICKET) String ticket, String title, String content, MultipartFile[] files, String[] childrenIds, Integer mediaType, Integer category, String coursePlanId){

        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        AreaDTO area = ssoCallService.getAreaDTO(ticket);

        LiveRecord record = liveRecordService.addLiveRecord(title, content, files, childrenIds, mediaType, category, coursePlanId, staff, area);
        return StatusResponse.success(record);
    }

    @PostMapping("/add_plain")
    public StatusResponse addLiveRecord(@RequestHeader(TICKET) String ticket, String title, String content, String resourceUrl,
                                        String[] childrenIds, Integer mediaType, Integer category, String coursePlanId){

        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        AreaDTO area = ssoCallService.getAreaDTO(ticket);

        LiveRecord record = liveRecordService.addLiveRecord(title, content, resourceUrl, childrenIds, mediaType, category, coursePlanId, staff, area);
        return StatusResponse.success(record);
    }

    @RequestMapping("/children_by_plan")
    public StatusResponse getChildrenOfClasses(String coursePlanId, Integer category){
        Map result = liveRecordService.getChildrenByCourse(coursePlanId, category);
        List<Map> list = new ArrayList<>();
        list.add(result);
        return StatusResponse.success(list);
    }

    @RequestMapping("/statistics_info")
    public StatusResponse getRecordList(@RequestHeader(TICKET) String ticket){
        BaseInfoVo baseInfoVo = liveRecordService.statisticsInfo(cache.getStaff(ticket), ssoCallService.getAreaDTO(ticket));
        return StatusResponse.success(baseInfoVo);
    }

    @RequestMapping("/record_page")
    public StatusResponse getRecordList(@RequestHeader(TICKET) String ticket, PagerQuery query){
        Page<RecordListVo> recordPage = liveRecordService.getRecordPage(cache.getStaffId(ticket), query.getPageNum(), query.getPageSize());
        return StatusResponse.success(recordPage);
    }

    @RequestMapping("/record_page_by_areaId")
    public StatusResponse getRecordListByAreaId(@RequestHeader(TICKET) String ticket, PagerQuery query){
        String areaId = cache.getAreaId(ticket);
        Page<RecordListVo> recordPage = liveRecordService.getRecordPageByAreaId(areaId, query.getPageNum(), query.getPageSize());
        return StatusResponse.success(recordPage);
    }

    @RequestMapping("/all_children")
    public StatusResponse allChildren(@RequestHeader(TICKET) String ticket) {
        List<LiveRecordClassesVo> childrenList = liveRecordService.getChildrenByArea(cache.getAreaId(ticket));
        return StatusResponse.success(childrenList);
    }

    @RequestMapping("/record_by_plan")
    public StatusResponse getLiveRecordByPlanId(String coursePlanId, PagerQuery query){
        Page<RecordListVo> recordPage = liveRecordService.getLiveRecordByPlanId(coursePlanId, query.getPageNum(), query.getPageSize());
        return StatusResponse.success(recordPage);
    }

    @RequestMapping("/deleteLiveRecord")
    public StatusResponse deleteLiveRecord(@RequestHeader(RequestConstant.TICKET) String ticket, String recordId) {
        LiveRecord liveRecord =  liveRecordService.deleteLiveRecord(cache.getStaff(ticket), recordId);
        return StatusResponse.success(ModelUtil.mapOf("recordId", liveRecord.getId(), "deleteStatus", liveRecord.getDeleted()));
    }
}
